<template>
	<view class="preview">
		<swiper circular :current="currentIndex" @change="curChange">
			<swiper-item v-for="(item,index) in storClassList" :key="item._id">
			
					<image v-if="readImgs.includes(index)" @click="maskChange"  :src="item.picurl" mode="aspectFill"></image>
			
			</swiper-item>
			
		</swiper>
		
		<view class="mask" v-show="maskState">
			<view class="goBack" :style="{top:statusBarHeight()+'px'}" @click="goBack">
				
				<uni-icons type="back" size="20" color="#fff"></uni-icons>
			</view>
			<view class="count">{{currentIndex+1}}/{{storClassList.length}}</view>
			<view class="time">
			
				<uni-dateformat :date="new Date()" format="hh:mm"></uni-dateformat>
				


			</view>
			<view class="date">
				<uni-dateformat :date="new Date()" format="MM月dd日"></uni-dateformat>
				
			</view>
			<view class="footer">
				<view class="box" @click="clickInfo">
					<uni-icons type="calendar" size="28" color="#333"></uni-icons>
					<view class="text">
						信息
					</view>
				</view>
				<view class="box" @click="clickStar">
					<uni-icons type="calendar" size="28" color="#333"></uni-icons>
					<view class="text">
						{{currentImgInfo.score}}分
					</view>
				</view>
				<view class="box" @click="imgDown">
					<uni-icons type="calendar" size="28" color="#333"></uni-icons>
					<view class="text">
						下载
					</view>
				</view>
			</view>
			
		</view>
		
		<!-- //弹窗 -->
		<!-- :safe-area="false" 修改安全镂空区域 -->
		<uni-popup ref="infoPopup" type="bottom" :safe-area="false">
			<view class="infoPopup">
				<view class="popheader">
					<view></view>
					<view class="title">壁纸信息</view>
					<view class="close" @click="closeInfo">X</view>
				</view>
				
				<scroll-view scroll-y>
						<view class="content">
							<view class="row" v-for="item in 10">
								
									<view class="left">
										我的小时
									</view>
									<view class="right">
										<uni-rate v-model="value" readonly></uni-rate>
									</view>
							</view>
							
							<!-- 
							 -->
							<view class="safe-area-inset-bottom">
								
							</view>
							
						</view>
				</scroll-view>
			
			</view>
			
		</uni-popup>
		
		<!-- //评分 -->
		<uni-popup ref="starPopup">
			<view class="starPopup">
				<view class="popheader">
					<view></view>
					<view class="title">{{currentImgInfo.userScore?"已经评分了":"请评分"}}</view>
					<view class="close" @click="closeStar">X</view>
				</view>
				<view class="content">
					<uni-rate v-model="userStar"></uni-rate>
					<view class="text">{{userStar}}分</view>
				</view>
				
				<view class="footer">
					<button :disabled="!userStar || isScore" type="default" size="mini" plain @click="sub">确认评分</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import {onLoad} from '@dcloudio/uni-app';
import {statusBarHeight} from '@/utils/system.js';
import {apiSetScore} from '../../api/apis.js';
const maskState =ref(true);
const infoPopup = ref(null);
const starPopup= ref(null);
const value =ref(2.5);
const userStar = ref(0);
const currentIndex = ref(0);
const readImgs = ref([]);
//定义当前图片信息
const currentImgInfo = ref([]);
//获取缓存
const storClassList = ref(uni.getStorageSync('storClassList'));
//定义是否评分过
const isScore = ref(false);


onLoad((e)=>{
	
	currentIndex.value = storClassList.value.findIndex(item=>item._id == e.id);
	currentOver();//获取左右页码
	//定义当前图片信息
	 currentImgInfo.value = storClassList.value[currentIndex.value];
	

	 //评分
	 userStar.value= currentImgInfo.value.userScore?currentImgInfo.value.userScore:0;
	 isScore.value = userStar.value;
})

function maskChange(){
	
	maskState.value = !maskState.value;
}

function clickInfo(){
	infoPopup.value.open();
}

function closeInfo(){
	infoPopup.value.close();
}

function clickStar(){
	starPopup.value.open();
}

function closeStar(){
	starPopup.value.close();
}
//执行评分
const sub = async ()=>{
	//mask设置后没有效果 这是一个bug
	uni.showLoading({
		title:"提交中...",
		mask:true
	})
	let {classid,_id:wallId} = currentImgInfo.value;
	
	let data = {
		classid,
		wallId,
		userScore:userStar.value
	}
	
	let res = await apiSetScore(data);
	uni.hideLoading();
	if(res.data.errCade==0){
		uni.showToast({
			title:res.data.errMsg,
			icon:"none"
		})
	}
	isScore.value = true;
	storClassList.value[currentIndex.value].userScore = userStar.value;
	
	//重写到缓存
	uni.setStorageSync('storClassList',storClassList.value);
	
}

function goBack(){
	uni.navigateBack()
}

//当前页面左右页
function currentOver(){
		readImgs.value = [
			currentIndex.value<=0?storClassList.value.length-1:currentIndex.value-1,
			currentIndex.value,
			currentIndex.value>=storClassList.value.length - 1? 0 : currentIndex.value + 1
		];
}

//滑动改页码
function curChange(e){
	currentIndex.value = e.detail.current;
	currentOver();//获取左右页码
	//滑动定义当前图片信息
	currentImgInfo.value = storClassList.value[currentIndex.value];
	
	//评分
	userStar.value= currentImgInfo.value.userScore?currentImgInfo.value.userScore:0;
	isScore.value = userStar.value;
}

//点击下载图片
const imgDown = () =>{
	// #ifdef H5
		uni.showModal({
			content:"请长按保存",
			showCancel:false
		})
	// #endif
	
	// #ifndef H5
		uni.getImageInfo({
			src:currentImgInfo.value.picurl,
			success:(res) =>{
				
				uni.saveImageToPhotosAlbum({
					filePath:res.path,
					success:(res1) =>{
						
						
						if(res1.errMsg == "saveImageToPhotosAlbum:ok"){
							uni.showToast({
								title:'下载成功',
								icon:"none"
							})
						}
					
					},
					
					fail:(err)=>{
					
						
						if(err.errMsg == "saveImageToPhotosAlbum:fail cancel"){
							uni.showToast({
								title:'取消下载',
								icon:"none"
							})
						}
						
						if(err.errMsg == "saveImageToPhotosAlbum:fail auth deny"){
							//如果没有授权过
							uni.showModal({
								title:"授权消息",
								content:"请授权",
								success:(res2)=>{
									
									//调用授权
									if(res2.confirm){
										uni.openSetting({
											success:(setting)=>{
											
												if(setting.authSetting['scope.writePhotosAlbum']){
													uni.showToast({
														title:'授权成功',
														icon:"none"
													})
												}else{
													uni.showToast({
														title:'授权失败',
														icon:"none"
													})
												}
												
											},
											
										})
									}
								}
							})
						}else{
							//取消授权
						}
					}
				})
			}
		})
	// #endif
}


</script>

<style lang='scss' scoped>
	.preview{
		width: 100%;
		height: 100vh;
		position: relative;
		swiper{
			width: 100%;
			height: 100%;
			image{
				width: 100%;
				height: 100%;
			}
		}
		
		.popheader{
			display: flex;
			justify-content: space-between;
			align-items: center;
			.title{
				color:green;
				font-size: 28rpx;
			}
			.close{
				padding: 4rpx;
			}
		}
		
		
		.mask{
			&>view{
				position: absolute;
				left:0;
				right: 0;
				margin:auto;
				width: fit-content;
				color:#fff;
			}
			.goBack{
				width: 64rpx;
				height: 64rpx;
				background: rgba(0, 0,0,0.5);
				left:30rpx;
				top:0;
				margin-left: 0;
				border-radius: 100rpx;
				backdrop-filter: blur(10rpx);
				/* border:1rpx solid rgba(255, 255,255,0.3); */
				display: flex;
				justify-content: center;
				align-items: center;
			}
			.count{
				top:10vh;
				border-radius: 40rpx;
				font-size:28rpx;
				background: rgba(0, 0,0,0.3);
				backdrop-filter:blur(10rpx);
				padding: 8rpx 20rpx;
			}
			
			.time{
				top:calc(10vh + 80rpx);
				font-size:120rpx;
				font-weight: 100;
				line-height: 1em;
				text-shadow: 0 4rpx rgba(0, 0,0,0.3);
			}
			
			.date{
				top:calc(10vh + 210rpx);
				font-size:34rpx;
				font-weight: 100;
				line-height: 1em;
				text-shadow: 0 4rpx rgba(0, 0,0,0.3);
			}
			
			.footer{
				background:rgba(255, 255,255,0.8);
				bottom:10vh;
				display: flex;
				justify-content: space-around;
				align-items: center;
				width:65vw;
				border-radius: 60rpx;
				height: 120rpx;
				backdrop-filter:blur(10rpx);
				.box{
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					font-size: 28rpx;
					padding:2rpx 15rpx;
					color:#333;
				}
			}
			
			
		}
		.infoPopup{
			background:#fff;
			padding: 30rpx;
			border-radius: 30rpx 30rpx 0 0;
			overflow: hidden;
			width:100vw;
		
			scroll-view{
				max-height: 60vh;
			}
			
			.content{
				.row{
					display: flex;
					justify-content: space-between;
					align-items: center;
					line-height: 1.7em;
					font-size: 32rpx;
					.left{
						color:red;
						width:140rpx;
						text-align: right;
						padding-right:5rpx;
					}
					.right{
						flex: 1;
						width:0;
						word-wrap: break-word;
						padding-left:5rpx;
					}
				}
			}
		}
		
		.starPopup{
			width:65vw;
			background: #fff;
			border-radius:20rpx;
			padding: 30rpx;
			.content{
				display: flex;
				justify-content: center;
				align-items: center;
				padding: 60rpx;
				.text{
					padding-left: 20rpx;
					font-size: 32rpx;
					line-height: 1.2em;
					text-align: center;
					color: yellowgreen;
					
				}
			}
			
			.footer{
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
		
		
	}    
</style>
